package gov.pbc.nn.core.bdf.view.frame;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.bdf2.core.view.frame.IFrameShortcutActionRegister;
import com.bstek.bdf2.uflo.proxy.service.ProxyService;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.view.widget.Container;
import com.bstek.dorado.view.widget.SubViewHolder;
import com.bstek.uflo.model.task.TaskState;
import com.bstek.uflo.query.TaskQuery;
import com.bstek.uflo.service.TaskService;

public class FetchTodoTaskCountShortcutRegister implements IFrameShortcutActionRegister {

	@Autowired
	@Qualifier("uflo.taskService")
	private TaskService taskService;
	@Autowired
	@Qualifier("uflo.proxy.proxyService")
	private ProxyService proxyService;
	@Value("${bdf2.uflo.taskproxy.disabledFetchTodoTaskCountShortcutRegister}")
	private boolean disabled;

	public void registerToFrameTop(Container container) {
	}

	@Expose
	public Map<String, Object> loadTodoTaskCount() {
		IUser user = ContextHolder.getLoginUser();
		if (user == null) {
			throw new NoneLoginException("Please login first.");
		}
		Map<String, Object> map = new HashMap();
		TaskQuery ownerQuery = this.taskService.createTaskQuery();
		ownerQuery.assignee(user.getUsername());
		map.put("personal", Integer.valueOf(ownerQuery.count()));

		TaskQuery groupQuery = this.taskService.createTaskQuery();
		groupQuery.addParticipator(user.getUsername());
		groupQuery.addTaskState(TaskState.Ready);
		map.put("group", Integer.valueOf(groupQuery.count()));

		map.put("proxy", Integer.valueOf(this.proxyService.findProxyTaskCount(user.getUsername())));
		return map;
	}

	public void registerToStatusBar(Container container) {
		SubViewHolder subviewHolder = new SubViewHolder();
		subviewHolder.setSubView("gov.pbc.nn.core.bdf.view.frame.FetchTodoTaskCount");
		container.addChild(subviewHolder);
	}

	public boolean isDisabled() {
		return this.disabled;
	}

	public int order() {
		return 1000;
	}

}
